package org.example.library;

import com.google.gson.Gson;
import org.example.methods.Methods;

import java.io.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet(value = "/CF1")
public class CF1 extends HttpServlet {


    public void init() {

    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");// 设置响应内容类型为 HTML
        request.setCharacterEncoding("UTF-8");// 设置请求字符编码为 UTF-8
        response.setContentType("text/html; charset=UTF-8");// 再次设置响应内容类型为 HTML，并指定字符编码为 UTF-8
// 获取用于写响应内容的 PrintWriter 对象
        PrintWriter out=response.getWriter();
        try {// 调用方法 CF1，获取包含数据的 HashMap

            HashMap<String, ArrayList<?>> list = Methods.CF1();
            // 使用 Gson 将 HashMap 转换为 JSON 字符串
            Gson gson = new Gson();
            String json = gson.toJson(list);
            // 将 JSON 字符串写入响应out.println(json);
        } catch (ClassNotFoundException e) {// 捕获 ClassNotFoundException 异常，并抛出 RuntimeException
            throw new RuntimeException(e);// 捕获 SQLException 异常，并抛出 RuntimeException
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    public void destroy() {
    }
}